home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / VertexBlend / vertexblend.frm < prev    next >
Text File  |  2001-10-08  |  13KB  |  419 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Vertex Blend"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5640
  8.    Icon            =   "vertexblend.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   299
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   376
  13.    StartUpPosition =   3  'Windows Default
  14. End
  15. Attribute VB_Name = "Form1"
  16. Attribute VB_GlobalNameSpace = False
  17. Attribute VB_Creatable = False
  18. Attribute VB_PredeclaredId = True
  19. Attribute VB_Exposed = False
  20. '-----------------------------------------------------------------------------
  21. ' File: VertexBlend.frm
  22. '
  23. ' Desc: Example code showing how to do a skinning effect, using the vertex
  24. '       blending feature of Direct3D. Normally, Direct3D transforms each
  25. '       vertex through the world matrix. The vertex blending feature,
  26. '       however, uses mulitple world matrices and a per-vertex blend factor
  27. '       to transform each vertex.
  28. '
  29. ' Copyright (C) 1999-2001 Microsoft Corporation. All rights reserved.
  30. '-----------------------------------------------------------------------------
  31.  
  32.  
  33. Option Explicit
  34.  
  35. Const D3DVBF_DISABLE = 0    '     Disable vertex blending
  36. Const D3DVBF_1WEIGHTS = 1   '     2 matrix blending
  37. Const D3DVBF_2WEIGHTS = 2   '     3 matrix blending
  38. Const D3DVBF_3WEIGHTS = 3   '     4 matrix blending
  39. Const D3DVBF_0WEIGHTS = 256 '     one matrix is used with weight 1.0
  40.  
  41. '-----------------------------------------------------------------------------
  42. ' Name: struct D3DBLENDVERTEX
  43. ' Desc: Custom vertex which includes a blending factor
  44. '-----------------------------------------------------------------------------
  45. Private Type D3DBLENDVERTEX
  46.  
  47.     v As D3DVECTOR
  48.     blend As Single
  49.     n As D3DVECTOR
  50.     tu As Single
  51.     tv As Single
  52.     
  53. End Type
  54.  
  55. Const D3DFVF_BLENDVERTEX = (D3DFVF_XYZB1 Or D3DFVF_NORMAL Or D3DFVF_TEX1)
  56.  
  57. Dim m_Object As CD3DMesh
  58. Dim m_matUpperArm As D3DMATRIX
  59. Dim m_matLowerArm As D3DMATRIX
  60. Dim m_mediadir As String
  61. Dim g_ftime As Single
  62.  
  63. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  64. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  65.  
  66.  
  67. '-----------------------------------------------------------------------------
  68. ' Name: Form_Load()
  69. ' Desc:
  70. '-----------------------------------------------------------------------------
  71. Private Sub Form_Load()
  72.     
  73.     
  74.     ' Show the form
  75.     Me.Show
  76.     DoEvents
  77.  
  78.     
  79.     Me.Caption = "VertexBlend: Surface Skinning Example"
  80.         
  81.  
  82.     
  83.     ' Initialize D3D
  84.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  85.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  86.     ' If all fail it will display a message box indicating so.
  87.     '
  88.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  89.     If Not (m_bInit) Then End
  90.             
  91.     
  92.     ' Find and set the path to our media
  93.     m_mediadir = FindMediaDir("mslogo.x")
  94.     D3DUtil_SetMediaPath m_mediadir
  95.     
  96.     ' Create new D3D mesh objects and loads content from disk
  97.     InitDeviceObjects
  98.     
  99.     ' Sets the state for those objects and the current D3D device
  100.     RestoreDeviceObjects
  101.     
  102.     ' Start our timer
  103.     DXUtil_Timer TIMER_start
  104.     
  105.     ' Run the simulation forever
  106.     ' See Form_Keydown for exit processing
  107.     Do While True
  108.     
  109.         ' Increment the simulation
  110.         FrameMove
  111.         
  112.         ' Render one image of the simulation
  113.         If Render Then
  114.         
  115.             ' Present the image to the screen
  116.             D3DUtil_PresentAll g_focushwnd
  117.         End If
  118.         
  119.         ' Allow for events to get processed
  120.         DoEvents
  121.         
  122.     Loop
  123.     
  124. End Sub
  125.  
  126. '-----------------------------------------------------------------------------
  127. ' Name: Form_KeyDown()
  128. ' Desc: Process key messages for exit and change device
  129. '-----------------------------------------------------------------------------
  130. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  131.      Select Case KeyCode
  132.         
  133.         Case vbKeyEscape
  134.             Unload Me
  135.             
  136.         Case vbKeyF2
  137.                 
  138.             ' Pause the timer
  139.             DXUtil_Timer TIMER_STOP
  140.             
  141.             ' Bring up the device selection dialog
  142.             ' we pass in the form so the selection process
  143.             ' can make calls into InitDeviceObjects
  144.             ' and RestoreDeviceObjects
  145.             frmSelectDevice.SelectDevice Me
  146.             
  147.             ' Restart the timer
  148.             DXUtil_Timer TIMER_start
  149.             
  150.         Case vbKeyReturn
  151.         
  152.             ' Check for Alt-Enter if not pressed exit
  153.             If Shift <> 4 Then Exit Sub
  154.             
  155.             ' If we are windowed go fullscreen
  156.             ' If we are fullscreen returned to windowed
  157.             If g_d3dpp.Windowed Then
  158.                  D3DUtil_ResetFullscreen
  159.             Else
  160.                  D3DUtil_ResetWindowed
  161.             End If
  162.                              
  163.             ' Call Restore after ever mode change
  164.             ' because calling reset looses state that needs to
  165.             ' be reinitialized
  166.             RestoreDeviceObjects
  167.            
  168.     End Select
  169. End Sub
  170.  
  171.  
  172. '-----------------------------------------------------------------------------
  173. ' Name: Form_Resize()
  174. ' Desc: hadle resizing of the D3D backbuffer
  175. '-----------------------------------------------------------------------------
  176. Private Sub Form_Resize()
  177.  
  178.     ' If D3D is not initialized then exit
  179.     If Not m_bInit Then Exit Sub
  180.     
  181.     ' If we are in a minimized state stop the timer and exit
  182.     If Me.WindowState = vbMinimized Then
  183.         DXUtil_Timer TIMER_STOP
  184.         m_bMinimized = True
  185.         Exit Sub
  186.         
  187.     ' If we just went from a minimized state to maximized
  188.     ' restart the timer
  189.     Else
  190.         If m_bMinimized = True Then
  191.             DXUtil_Timer TIMER_start
  192.             m_bMinimized = False
  193.         End If
  194.     End If
  195.     
  196.     ' Dont let the window get too small
  197.     If Me.ScaleWidth < 10 Then
  198.         Me.width = Screen.TwipsPerPixelX * 10
  199.         Exit Sub
  200.     End If
  201.     
  202.     If Me.ScaleHeight < 10 Then
  203.         Me.height = Screen.TwipsPerPixelY * 10
  204.         Exit Sub
  205.     End If
  206.     
  207.     
  208.     'reset and resize our D3D backbuffer to the size of the window
  209.     D3DUtil_ResizeWindowed Me.hwnd
  210.     
  211.     'All state get losts after a reset so we need to reinitialze it here
  212.     g_lWindowWidth = Me.ScaleWidth
  213.     g_lWindowHeight = Me.ScaleHeight
  214.     RestoreDeviceObjects
  215.     
  216. End Sub
  217.  
  218.  
  219. '-----------------------------------------------------------------------------
  220. ' Name: Form_Unload()
  221. ' Desc:
  222. '-----------------------------------------------------------------------------
  223. Private Sub Form_Unload(Cancel As Integer)
  224.     DeleteDeviceObjects
  225.     End
  226. End Sub
  227.  
  228. '-----------------------------------------------------------------------------
  229. ' Name: FrameMove()
  230. ' Desc: Called once per frame, the call is the entry point for animating
  231. '       the scene.
  232. '-----------------------------------------------------------------------------
  233. Sub FrameMove()
  234.     g_ftime = DXUtil_Timer(TIMER_GETAPPTIME)
  235.  
  236.     ' Set the vertex blending matrices for this frame
  237.     D3DXMatrixIdentity m_matUpperArm
  238.     Dim vAxis As D3DVECTOR
  239.     vAxis = vec3(2 + Sin(g_ftime * 3.1), 2 + Sin(g_ftime * 3.3), Sin(g_ftime * 3.5))
  240.     
  241.     D3DXMatrixRotationAxis m_matLowerArm, vAxis, Sin(3 * g_ftime)
  242.  
  243. End Sub
  244.  
  245.  
  246.  
  247. '-----------------------------------------------------------------------------
  248. ' Name: Render()
  249. ' Desc: Called once per frame, the call is the entry point for 3d
  250. '       rendering. This function sets up render states, clears the
  251. '       viewport, and renders the scene.
  252. '-----------------------------------------------------------------------------
  253. Function Render() As Boolean
  254.     Dim hr As Long
  255.     
  256.     Render = False
  257.     'See what state the device is in.
  258.     hr = g_dev.TestCooperativeLevel
  259.     If hr = D3DERR_DEVICENOTRESET Then
  260.         g_dev.Reset g_d3dpp
  261.         RestoreDeviceObjects
  262.     End If
  263.     
  264.     'dont bother rendering if we are not ready yet
  265.     If hr <> 0 Then Exit Function
  266.     Render = True
  267.  
  268.     ' Clear the backbuffer
  269.     D3DUtil_ClearAll &HFF&
  270.     
  271.     With g_dev
  272.         .BeginScene
  273.         
  274.     
  275.         ' Enable vertex blending
  276.         .SetRenderState D3DRS_VERTEXBLEND, D3DVBF_1WEIGHTS
  277.         .SetTransform D3DTS_WORLD, m_matUpperArm
  278.         .SetTransform D3DTS_WORLD1, m_matLowerArm
  279.         
  280.         ' Display the object
  281.         m_Object.Render g_dev
  282.  
  283.         ' End the scene.
  284.         .EndScene
  285.     End With
  286.  
  287. End Function
  288.  
  289.  
  290.  
  291. '-----------------------------------------------------------------------------
  292. ' Name: InitDeviceObjects()
  293. ' Desc: Initialize scene objects.
  294. '-----------------------------------------------------------------------------
  295. Function InitDeviceObjects() As Boolean
  296.     
  297.     Dim b As Boolean
  298.     
  299.     Set m_Object = New CD3DMesh
  300.     
  301.     b = m_Object.InitFromFile(g_dev, m_mediadir + "mslogo.x")
  302.     
  303.     If Not b Then
  304.         MsgBox "media not found"
  305.         End
  306.     End If
  307.     
  308.     
  309.     ' Set a custom FVF for the mesh
  310.     m_Object.SetFVF g_dev, D3DFVF_BLENDVERTEX
  311.     
  312.     Dim VertB As Direct3DVertexBuffer8
  313.     Dim Vertices() As D3DBLENDVERTEX
  314.     Dim NumVertices As Long
  315.     Dim MinX As Single
  316.     Dim MaxX As Single
  317.     Dim a As Single
  318.     Dim i As Long
  319.     
  320.     NumVertices = m_Object.mesh.GetNumVertices()
  321.     Set VertB = m_Object.mesh.GetVertexBuffer()
  322.     
  323.     
  324.     MinX = 10000000000#
  325.     MaxX = -10000000000#
  326.     
  327.     ReDim Vertices(NumVertices)
  328.     
  329.     'copy data into our own array
  330.     D3DVertexBuffer8GetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  331.     
  332.     
  333.     ' Calculate the min/max z values for all the vertices
  334.     For i = 0 To NumVertices - 1
  335.         If Vertices(i).v.x < MinX Then MinX = Vertices(i).v.x
  336.         If Vertices(i).v.x > MaxX Then MaxX = Vertices(i).v.x
  337.     Next
  338.     
  339.     ' Set the blend factors for the vertices
  340.     
  341.     For i = 0 To NumVertices - 1
  342.         a = (Vertices(i).v.x - MinX) / (MaxX - MinX)
  343.         Vertices(i).blend = 1 - Sin(a * g_pi * 1)
  344.     Next
  345.     
  346.  
  347.     D3DVertexBuffer8SetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  348.     
  349.     Set VertB = Nothing
  350.     
  351.     InitDeviceObjects = True
  352.     
  353.     
  354. End Function
  355.  
  356. '-----------------------------------------------------------------------------
  357. ' Name: RestoreDeviceObjects()
  358. ' Desc: Restore device-memory objects and state after a device is created or
  359. '       resized.
  360. '-----------------------------------------------------------------------------
  361. Sub RestoreDeviceObjects()
  362.  
  363.     ' Restore mesh's local memory objects
  364.     m_Object.RestoreDeviceObjects g_dev
  365.  
  366.     ' Set miscellaneous render states
  367.     With g_dev
  368.         .SetRenderState D3DRS_ZENABLE, 1  'TRUE
  369.         .SetRenderState D3DRS_AMBIENT, &H444444
  370.         
  371.  
  372.         ' Set the projection matrix
  373.         Dim matProj As D3DMATRIX
  374.         D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, Me.ScaleHeight / Me.ScaleWidth, 1#, 10000#
  375.         .SetTransform D3DTS_PROJECTION, matProj
  376.  
  377.         ' Set the app view matrix for normal viewing
  378.         Dim vEyePt As D3DVECTOR, vLookatPt As D3DVECTOR, vUpVec As D3DVECTOR
  379.         Dim matView As D3DMATRIX
  380.         vEyePt = vec3(0#, -5#, -10#)
  381.         vLookatPt = vec3(0#, 0#, 0#)
  382.         vUpVec = vec3(0#, 1#, 0#)
  383.         D3DXMatrixLookAtLH matView, vEyePt, vLookatPt, vUpVec
  384.         .SetTransform D3DTS_VIEW, matView
  385.  
  386.         ' Create a directional light
  387.         Dim light As D3DLIGHT8
  388.         D3DUtil_InitLight light, D3DLIGHT_DIRECTIONAL, 1, -1, 1
  389.         .SetLight 1, light
  390.         .LightEnable 1, 1  'True
  391.         .SetRenderState D3DRS_LIGHTING, 1 'TRUE
  392.         
  393.         
  394.     End With
  395.     
  396. End Sub
  397.  
  398.  
  399. '-----------------------------------------------------------------------------
  400. ' Name: InvalidateDeviceObjects()
  401. ' Desc: Called when the device-dependant objects are about to be lost.
  402. '-----------------------------------------------------------------------------
  403. Sub InvalidateDeviceObjects()
  404.     m_Object.InvalidateDeviceObjects
  405. End Sub
  406.  
  407.  
  408. '-----------------------------------------------------------------------------
  409. ' Name: DeleteDeviceObjects()
  410. ' Desc: Called when the app is exitting, or the device is being changed,
  411. '       this function deletes any device dependant objects.
  412. '-----------------------------------------------------------------------------
  413. Sub DeleteDeviceObjects()
  414.     m_Object.Destroy
  415.     Set m_Object = Nothing
  416.     m_bInit = False
  417. End Sub
  418.  
  419.